<template>
	<view class="preview">
		<swiper circular :current="currentIndex" @change="swiperChange">
			<swiper-item v-for="item in classList" :key="item._id">
				<image @click="maskChange" :src="item.picurl" mode="aspectFill"></image>
			</swiper-item>
		</swiper>
		
		<!-- 遮罩层 -->
		<view class="mask" @click="maskChange" v-show="maskState">
			<view class="goBack">
				<uni-icons type="left"></uni-icons>
			</view>
			<view class="count">{{currentIndex + 1}} / {{classList.length}}</view>
			<view class="time">
				<uni-dateformat :date="Date.now()" format="hh:mm"></uni-dateformat>
			</view>
			<view class="date">
				<uni-dateformat :date="Date.now()" format="MM月dd日"></uni-dateformat>
			</view>
			<view class="footer">
				<view class="box" @click="clickInfo">
					<uni-icons type="info" size="28"></uni-icons>
					<view class="text">消息</view>
				</view>
				<view class="box" @click="clickScore">
					<uni-icons type="star" size="28"></uni-icons>
					<view class="text">{{currentInfo.score}}分</view>
				</view>
				<view class="box" @click="clickDownload">
					<uni-icons type="download" size="23"></uni-icons>
					<view class="text">下载</view>
				</view>
			</view>
		</view>
	
		<!-- 图片信息弹窗 -->
		<uni-popup ref="infoPopup" type="bottom">
			<view class="infoPopup">
				<view class="popHeader">
					<view></view>
					<view class="title">壁纸信息</view>
					<view class="close" @click="clickInfoClose">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<scroll-view scroll-y>
					<view class="content">
						<view class="row">
							<text class="label">壁纸ID：</text>
							<text selectable class="value">{{currentInfo._id}}</text>
						</view>
<!-- 						<view class="row">
							<text class="label">分类：</text>
							<text selectable class="value">{{currentInfo}}</text>
						</view> -->
						<view class="row">
							<text class="label">发布者：</text>
							<text selectable class="value">{{currentInfo.nickname}}</text>
						</view>
						<view class="row">
							<view class="label">评分：</view>
							<view class="value roteBox">
								<uni-rate v-model="currentInfo.score" @change="onChange" readonly touchable></uni-rate>
								<text class="score">{{currentInfo.score}}分</text>
							</view>
						</view>
						<view class="row">
							<text class="label">摘要：</text>
							<text class="value">{{currentInfo.description}}</text>
						</view>
						<view class="row">
							<text class="label">标签：</text>
							<view class="value tabs">
								<view class="tab" v-for="item in currentInfo.tabs">
									{{item}}
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
	
		<!-- 评分弹窗 -->
		<uni-popup ref="scorePopup" :is-mask-click="false">
			<view class="scorePopup">
				<view class="popHeader">
					<view></view>
					<view class="title">壁纸评分</view>
					<view class="close" @click="clickScoreClose">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<view class="content">
					<uni-rate v-model="userScore" allowHalf @change="onChange"></uni-rate>
					<text class="text">{{ userScore }}</text>
				</view>
				<view class="footer">
					<button type="default" size="mini" plain :disabled="!userScore" @click="submitScore">确认评分</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad } from "@dcloudio/uni-app"
import { apiGetSetupScore,apiWriteDownload } from "@/api/api.js"

// 从localStorage中取出数据
const classList = ref([]);
const storgClassList = uni.getStorageSync("storgClassList") || [];
classList.value = storgClassList.map(item => {
	return {
		...item,
		picurl: item.smallPicurl.replace("_small.webp", ".jpg") // 小图转大图
	}
})

// 遮罩层
const maskState = ref(false)
function maskChange() {
	maskState.value = !maskState.value
}

// popup弹出框
const infoPopup = ref(null)
// 点击显示弹窗
function clickInfo() {
	infoPopup.value.open()
}

// 点击关闭弹窗
function clickInfoClose() {
	infoPopup.value.close()
}

const userScore = ref(0)
const scorePopup = ref(null)
// 点击显示评分弹窗
function clickScore() {
	if (currentInfo.value.userScore) {
		isScore.value = true;
		userScore.value = currentInfo.value.userScore;
	}
	scorePopup.value.open();
}
// 点击关闭评分弹窗
function clickScoreClose() {
	scorePopup.value.close()
	userScore.value = 0;
	isScore.value = false;
}
// 提交评分
async function submitScore() {
	uni.showLoading({
		title: "加载中..."
	})
	
	// 解构赋值（将_id取别名未wallId）
	let { classid, _id: wallId } = currentInfo.value;
	
	let res = await apiGetSetupScore({
		classid,
		wallId,
		userScore: userScore.value
	})
	
	uni.hideLoading();
	
	console.log(res)
	
	if (res.errCode === 0) {
		uni.showToast({
			title: "评分成功",
			icon: "none"
		})
		classList.value[currentIndex.value].userScore = userScore.value;
		uni.setStorageSync("storgClassList", classList.value);
		clickScoreClose();
	}

}

// 显示对应图片的信息
const currentInfo = ref(null)
const currentId = ref(null)
const currentIndex = ref(0)
// 获取页面传递过来的参数
onLoad((e) => {
	currentId.value = e.id
	currentIndex.value = classList.value.findIndex(item => {
		return item._id == currentId.value
	})
	currentInfo.value = classList.value[currentIndex.value]
})

// swiper事件
const swiperChange = (e) => {
	currentIndex.value = e.detail.current
	currentInfo.value = classList.value[currentIndex.value]
}

//点击下载
const clickDownload = async () => {

	// #ifdef H5
	uni.showModal({
		content: "H5端请长按保存壁纸",
		showCancel: false
	})
	// #endif

	// #ifndef H5
	try {

		uni.showLoading({
			title: "下载中...",
			mask: true
		})
		let {
			classid,
			_id: wallId
		} = currentInfo.value;
		let res = await apiWriteDownload({
			classid,
			wallId
		})
		if (res.errCode != 0) throw res;
		uni.getImageInfo({
			src: currentInfo.value.picurl,
			success: (res) => {
				console.log(res)
				
				// 保存图片到本地（网络图片转为本地路径保存）
				uni.saveImageToPhotosAlbum({
					filePath: res.path,
					success: (res) => {
						uni.showToast({
							title: "保存成功，请到相册查看",
							icon: "none"
						})
					},
					fail: err => {
						if (err.errMsg == 'saveImageToPhotosAlbum:fail cancel') {
							uni.showToast({
								title: '保存失败，请重新点击下载',
								icon: "none"
							})
							return;
						}
						uni.showModal({
							title: "授权提示",
							content: "需要授权保存相册",
							success: res => {
								if (res.confirm) {
									uni.openSetting({
										success: (setting) => {
											console.log(setting);
											if (setting.authSetting['scope.writePhotosAlbum']) {
												uni.showToast({
													title: "获取授权成功",
													icon: "none"
												})
											} else {
												uni.showToast({
													title: "获取权限失败",
													icon: "none"
												})
											}
										}
									})
								}
							}
						})
					},
					complete: () => {
						uni.hideLoading();
					}
				})
			}
		})
	} catch (err) {
		console.log(err);
		uni.hideLoading();
	}
	// #endif
}
</script>

<style lang="scss" scoped>
.preview{
	width: 100%;
	height: 100vh;
	position: relative;
	
	swiper{
		width: 100%;
		height: 100%;
		image{
			width: 100%;
			height: 100%;
		}
	}

	.mask{
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		
		.goBack{
			position: absolute;
			left: 3vh;
			top: 5vh;
			width: 50rpx;
			height: 50rpx;
			background-color: rgba(0, 0, 0, 0.7);
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			:deep(.uni-icons){
				color: #fff !important;
			}
		}
		.count{
			position: absolute;
			top: 10vh;
			left: 50%;
			transform: translate(-50%, 0);
			background-color: rgba(0, 0, 0, 0.3);
			backdrop-filter: blur(10rpx);
			font-size: 28rpx;
			color: #fff;
			border-radius: 40rpx;
			padding: 8rpx 28rpx;
		}
		.time{
			position: absolute;
			top: calc(10vh + 80rpx);
			left: 50%;
			transform: translate(-50%, 0);
			color: #fff;
			font-size: 140rpx;
			font-weight: 100;
			line-height: 1em;
			text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
		}
		.date{
			position: absolute;
			top: calc(10vh + 230rpx);
			left: 50%;
			transform: translate(-50%, 0);
			color: #fff;
			font-size: 34rpx;
			text-shadow: 0 2rpx rgba(0, 0, 0, 0.3);
		}
		.footer{
			position: absolute;
			bottom: 10vh;
			left: 50%;
			transform: translate(-50%, 0);
			background-color: rgba(255, 255, 255, 0.8);
			width: 65vw;
			height: 120rpx;
			border-radius: 120rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
			color: #000;
			box-shadow: 0 2rpx 0 rgba(0, 0, 0, 0.1);
			backdrop-filter: blur(10rpx);
			.box{
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 2rpx 12rpx;
				.text{
					font-size: 26rpx;
					color: $text-font-color-2;
				}
			}
		}
	}

	.infoPopup{
		background-color: #fff;
		padding: 30rpx;
		border-radius: 30rpx 30rpx 0 0;
		overflow: hidden;
		
		scroll-view{
			max-height: 60vh;
			.content{
				.row{
					display: flex;
					padding: 16rpx 0;
					font-size: 32rpx;
					line-height: 1.7em;
					.label{
						color: $text-font-color-3;
						width: 140rpx;
						text-align: right;
						font-size: 30rpx;
					}
					.value{
						width: 0;
						flex: 1;
					}
					.roteBox{
						display: flex;
						align-items: center;
						.score{
							font-size: 26rpx;
							color: $text-font-color-2;
							padding-left: 10rpx;
						}
					}
					.tabs{
						display: flex;
						flex-wrap: wrap;
						.tab{
							border: 1px solid $brand-theme-color;
							color: $brand-theme-color;
							font-size: 22rpx;
							padding: 10rpx 30rpx;
							border-radius: 40rpx;
							line-height: 1em;
						}
					}
				}
			}
		}
	}
	
	.popHeader{
		display: flex;
		justify-content: space-between;
		align-items: center;
		.title{
			color: $text-font-color-2;
			font-size: 26rpx
		}
		.close{
			padding: 6rpx; // 增加padding让按钮变得更容易选中
		}
	}

	.scorePopup{
		background-color: #fff;
		padding: 30rpx;
		width: 70vw;
		border-radius: 30rpx;
		.content{
			padding: 30rpx 0;
			display: flex;
			justify-content: center;
			align-items: center;
			.text{
				color: #feca3e;
				width: 80rpx;
				line-height: 1em;
				text-align: right;
			}
		}
		.footer{
			padding: 10rpx 0;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
}
</style>
